﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QLVT.Config;
using QLVT.Entity;

namespace QLVT.DAO
{
    class NhaCungCapDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("select * from NhaCungCap");
        }

        public NhaCungCap GetById(String mancc)
        {
            NhaCungCap ncc = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from NhaCungCap where mancc = '" + mancc + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    ncc = new NhaCungCap();
                    ncc.Mancc = dataReader.GetString(0);
                    ncc.Tenncc = dataReader.GetString(1);
                    ncc.Sdt = dataReader.GetString(2);
                    ncc.Email = dataReader.GetString(3);
                    return ncc;
                }
            }
            catch (Exception ex)
            {

            }
            finally {
                conn.Close();
                
            }
            return ncc;
        }
        public bool Insert(NhaCungCap nhaCungCap)
        {
            String sql = "INSERT INTO [QLVT].[dbo].[nhacungcap]([mancc],[tenncc],[sdt],[email])"
               + "VALUES('" + nhaCungCap.Mancc + "','" + nhaCungCap.Tenncc + "','" + nhaCungCap.Sdt + "','" + nhaCungCap.Email + "')";
            return Update(sql);
        }


        public bool DeleteById(String mancc)
        {
            return Update("delete from NhaCungCap where mancc = '" + mancc + "'");
        }

        public bool UpdateById(NhaCungCap ncc)
        {
            String sql = "update NhaCungCap "
                 + "set tenncc = '" + ncc.Tenncc + "'"
                 + ", sdt = '" + ncc.Sdt + "'"
                 + ", email = '" + ncc.Email + "'"
                 + " where mancc = '" + ncc.Mancc + "'";
            return Update(sql);
        }
    }
}
